﻿using System.Text;
using Common;
using Common.CipherTextHelper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Portal.Filter;
using Portal.Models;
using SoEasyPlatform.LayUiExample.Enties.DbModels;
using SqlSugar;

namespace Portal.Controllers
{
    /// <summary>
    /// 登录 业务逻辑控制器
    /// </summary>
    [LoginCheckFilter(IsCheck = false)]
    public class LoginController : MyBaseController
    {

        private readonly SqlSugarClient _sqlSugarClient;
        public LoginController(SqlSugarClient sqlSugarClient, IHostEnvironment hostingEnvironment) : base(hostingEnvironment)
        {
            _sqlSugarClient = sqlSugarClient;

        }


        /// <summary>
        /// 登录页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {

            return View();
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        public IActionResult VerificationCode()
        {
            var validateCode = new ValidateCode();
            var validateCodeStr = validateCode.CreateValidateCode(5);

            //将当前获取到的验证码写入到Session中. 作为后续的登录验证
            HttpContext.Session.Set(GlobalVariable.ValidateCode, Encoding.Default.GetBytes(validateCodeStr));
            var validateCodeBytes = validateCode.CreateValidateGraphic(validateCodeStr);
            return new FileContentResult(validateCodeBytes, "image/jpeg");
        }

        /// <summary>
        /// 登录请求
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="verificationCode">验证码</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ProcessLogin(string loginName, string pwd, string verificationCode)
        {

            if (string.IsNullOrEmpty(verificationCode))
            {
                return Content(JsonConvert.SerializeObject(new { Code = -1, Msg = "验证码不能为空" }));

            }

            if (verificationCode != HttpContext.Session.GetString(GlobalVariable.ValidateCode))
            {
                return Content(JsonConvert.SerializeObject(new { Code = -1, Msg = "验证码错误" }));
            }

            var md5Pwd = CipherTextHelper.GetMd5(pwd, Encoding.UTF8);


            var result = _sqlSugarClient.Queryable<sys_user>().First(x => x.loginname == loginName && x.pwd == md5Pwd);

            if (result == null)
            {
                return Content(JsonConvert.SerializeObject(new { Code = -1, Msg = "用户名或密码错误" }));
            }

            HttpContext.Session.SetString(GlobalVariable.LoginUser, JsonConvert.SerializeObject(result));

            //此处使用前台跳转方式
            return Content(JsonConvert.SerializeObject(new { Code = 200, Msg = "登录成功" }));
        }

        /// <summary>
        /// 登出请求
        /// </summary>
        /// <returns></returns>
        public void ProcessLoginOut()
        {
            //删除Session重定向到登录页

            HttpContext.Session.Clear();
            
            //重定向到登陆页面
            HttpContext.Response.Redirect("/Login/Index");

        }
    }
}